package com.fsck.k9.voice;

import java.util.ArrayList;

public class VoiceCommands {
	// fields 
	private ArrayList<String> masterCmdList;
	private ArrayList<String> inboxCmdList;
	private ArrayList<String> messageCmdList;
	private ArrayList<String> composeCmdList;
	
	private ArrayList<String> voiceRecCmdList;
	private static final int INBOX = 1;
	private static final int COMPOSE = 2;
	private static final int MESSAGE = 3;
	
	public VoiceCommands() {
		masterCmdList = new ArrayList<String>();
		inboxCmdList = new ArrayList<String>();
		messageCmdList = new ArrayList<String>();
		composeCmdList = new ArrayList<String>();
		
		// Adding the default Android Voice Action Commands
		masterCmdList.add("send text to");
		masterCmdList.add("navigate to");
		masterCmdList.add("call");
		masterCmdList.add("map of");
		masterCmdList.add("note to self");
		masterCmdList.add("listen to");
				
		// 
		masterCmdList.add("inbox");
		masterCmdList.add("exit");
		masterCmdList.add("quit");
		masterCmdList.add("home");
				
		// Adding commands that are only valid only within the inbox view
		inboxCmdList.add("read");
		inboxCmdList.add("reply");
		inboxCmdList.add("reply all");
		inboxCmdList.add("forward");
		inboxCmdList.add("delete");
		inboxCmdList.add("message");
				
		// Adding commands that are only valid within the message view
		messageCmdList.add("reply");
		messageCmdList.add("reply all");
		messageCmdList.add("forward");
		messageCmdList.add("delete");
		messageCmdList.add("subject");
		messageCmdList.add("sender");
		messageCmdList.add("message");
		messageCmdList.add("recipients");
		messageCmdList.add("body");
		messageCmdList.add("back");
		messageCmdList.add("return");
				
		// Adding commands that are only valid within the compose view
		composeCmdList.add("send");
		composeCmdList.add("save");
		composeCmdList.add("delete");
		composeCmdList.add("subject");
		composeCmdList.add("message");
		composeCmdList.add("recipient");
		composeCmdList.add("back");
		composeCmdList.add("return");
		composeCmdList.add("record");
	}
	
	public ArrayList<String> getVoiceRecCmdList() {
		return voiceRecCmdList;
	}

	public void setVoiceRecCmdList(ArrayList<String> voiceRecCmdList) {
		this.voiceRecCmdList = voiceRecCmdList;
	}
	
	/**
	 * Determines if 'wordList' contains a valid command
	 * @param wordList is the list of words to generated by the voice recognition activity
	 * @return 1 (true) if command is valid, otherwise return 0 (false)
	 * 
	 */
	public int verifyCommand(ArrayList<String> wordList) {

		setVoiceRecCmdList(wordList);
		
		// iterate through the first 4 words contained within the wordList and compare the word  
		for(int i = 0; (i < wordList.size()) || (i < 4); i++)
		{
			// check if the command at index 'i' is found within the list of valid commands
			for(int j = 0; j < masterCmdList.size(); j++) {
				if ( wordList.get(i).equals( masterCmdList.get(j) ) ) {
					return 1;
				}
			}
			// check if the command at index 'i' is found within the list of valid commands
			for(int j = 0; j < inboxCmdList.size(); j++) {
				if ( wordList.get(i).equals( inboxCmdList.get(j) ) ) {
					return 1;
				}
			}
			// check if the command at index 'i' is found within the list of valid commands
			for(int j = 0; j < messageCmdList.size(); j++) {
				if ( wordList.get(i).equals( messageCmdList.get(j) ) ) {
					return 1;
				}
			}
			// check if the command at index 'i' is found within the list of valid commands
			for(int j = 0; j < composeCmdList.size(); j++) {
				if ( wordList.get(i).equals( composeCmdList.get(j) ) ) {
					return 1;
				}
			}
		}
	
		// Was not able to match any of the words within 'wordList' to our current repository of valid commands
		return 0;
	}


	public int verifyCommand(ArrayList<String> wordList, int emailContextView) {
		
		if (emailContextView == INBOX) {
			// iterate through the first 4 words contained within the wordList and compare the word  
			for(int i = 0; (i < wordList.size()) || (i < 4); i++)
			{
				// check if the command at index 'i' is found within the list of valid commands
				for(int j = 0; j < masterCmdList.size(); j++) {
					if ( wordList.get(i).equals( masterCmdList.get(j) ) ) {
						handleInboxCmd(masterCmdList.get(j));
						return 1;
					}
				}
				// check if the command at index 'i' is found within the list of valid commands
				for(int j = 0; j < inboxCmdList.size(); j++) {
					if ( wordList.get(i).equals( inboxCmdList.get(j) ) ) {
						handleInboxCmd(masterCmdList.get(j));
						return 1;
					}
				}
			}
		} else if (emailContextView == MESSAGE) {
			// iterate through the first 4 words contained within the wordList and compare the word  
			for(int i = 0; (i < wordList.size()) || (i < 4); i++)
			{
				// check if the command at index 'i' is found within the list of valid commands
				for(int j = 0; j < masterCmdList.size(); j++) {
					if ( wordList.get(i).equals( masterCmdList.get(j) ) ) {
						return 1;
					}
				}
				// check if the command at index 'i' is found within the list of valid commands
				for(int j = 0; j < messageCmdList.size(); j++) {
					if ( wordList.get(i).equals( messageCmdList.get(j) ) ) {
						return 1;
					}
				}
			}
			
		} else if (emailContextView == COMPOSE) {
			// iterate through the first 4 words contained within the wordList and compare the word  
			for(int i = 0; (i < wordList.size()) || (i < 4); i++)
			{
				// check if the command at index 'i' is found within the list of valid commands
				for(int j = 0; j < masterCmdList.size(); j++) {
					if ( wordList.get(i).equals( masterCmdList.get(j) ) ) {
						return 1;
					}
				}
				// check if the command at index 'i' is found within the list of valid commands
				for(int j = 0; j < composeCmdList.size(); j++) {
					if ( wordList.get(i).equals( composeCmdList.get(j) ) ) {
						return 1;
					}
				}
			}
			
		} else {
			// iterate through the first 4 words contained within the wordList and compare the word  
			for(int i = 0; (i < wordList.size()) || (i < 4); i++)
			{
				// check if the command at index 'i' is found within the list of valid commands
				for(int j = 0; j < masterCmdList.size(); j++) {
					if ( wordList.get(i).equals( masterCmdList.get(j) ) ) {
						return 1;
					}
				}	
			}
			
		}
		
		return 0;
	}

	public void handleInboxCmd(String usrCmd) {
		

	}
}
